package com.winning.sx.microframework.common.util;

import java.util.Map;

/**
 * Created by GY on 2016/7/7.
 */
public class SqlUtil {
    public static String SqlWhere(Map<String, Object> map) {
        StringBuilder sqlWhere = new StringBuilder();
        if (map.get("ys_zd") != null) {
            if (!map.get("ys_zd").equals("-1") && map.get("ys_val") != null) {
                sqlWhere.append(" and ").append(map.get("ys_zd")).append(map.get("ys_op")).append(map.get("ys_val"));
            }
            map.remove("ys_zd");
            map.remove("ys_op");
            map.remove("ys_val");
        }

        for (Map.Entry<String, Object> entry : map.entrySet()) {
            if (entry.getKey().charAt(1) != '_') {
                sqlWhere.append(" and ").append(entry.getKey()).append(" = :").append(entry.getKey());
            } else {
                char type = entry.getKey().charAt(0);
                String key = entry.getKey().substring(2);

                switch (type) {
                    case 'b': //开始时间
                        sqlWhere.append(" and CONVERT(VARCHAR(10),").append(key).append(",120) >= :").append(entry.getKey());
                        break;
                    case 'e': //结束时间
                        sqlWhere.append(" and CONVERT(VARCHAR(10),").append(key).append(",120) <= :").append(entry.getKey());
                        break;
                    case 'a': //相等或者等于-1
                        sqlWhere.append(" and ('-1'=:").append(entry.getKey()).append(" or ISNULL(").append(key).append(",'0')=:").append(entry.getKey()).append(")");
                        break;
                    case 'l': //模糊
                        sqlWhere.append(" and ").append(key).append(" like '%").append(entry.getValue()).append("%'");
                        break;
                    case 'd': //相等
                        sqlWhere.append(" and ").append(key).append(" = :").append(entry.getKey());
                        break;
                    case 'u': //不等
                        sqlWhere.append(" and ").append(key).append(" <> :").append(entry.getKey());
                        break;
                    case 'n': //字段为空
                        if (!entry.getValue().equals("-1")) {
                            sqlWhere.append(" and (").append(entry.getValue()).append(" IS NULL OR ").append(entry.getValue()).append("='')");
                        }
                        break;
                    case 't': //日期date_format(dDiagnoseDate,'%Y-%m-%d')
                        sqlWhere.append(" and CONVERT(VARCHAR(10),").append(key).append(",120) = :").append(entry.getKey());
                        break;
                    case 's':
                        sqlWhere.append(" and ").append(key).append(">= :").append(entry.getKey());
                        break;
                    case 'm':
                        sqlWhere.append(" and ").append(key).append("<=:").append(entry.getKey());
                        break;
                    default:
                        sqlWhere.append(" and ").append(entry.getKey()).append(" = :").append(entry.getKey());
                }
            }
        }
        return sqlWhere.toString();
    }
}
